package cn.htu.edu.controller;


import cn.htu.edu.common.ResponseHelper;
import cn.htu.edu.common.ResponseModel;
import cn.htu.edu.entity.Orders;
import cn.htu.edu.entity.Users;
import cn.htu.edu.mapper.UsersMapper;
import cn.htu.edu.payload.request.users.UsersDto;
import cn.htu.edu.service.IUsersService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dakai
 * @since 2024-05-10
 */
@RestController
@Api(value = "联表接口",tags = "联表接口")
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private IUsersService iUsersService;

    @Resource
    private UsersMapper usersMapper;

    @ApiOperation("新增两表数据")
    @PostMapping("/saveUsers")
    public ResponseModel saveTeacher(@RequestBody Users users){
        return iUsersService.saveUsers(users);
    }

    @ApiOperation("全部查询")
    @GetMapping("/select")
    public ResponseModel select() {
        List<Users> users = iUsersService.getAllUsers();
        return ResponseHelper.buildResponseModel(users);
    }

    @ApiOperation("分页查询")
    @GetMapping("/selectUser")
    public ResponseModel<IPage<Users>> getAllUsers(Integer pageNo, Integer pageSize) {
        IPage<Users> p = new Page<>(pageNo, pageSize);
        List<Users> users = usersMapper.getAllUsers(p).getRecords();
        for (Users user:users) {
            List<Orders> orders = usersMapper.getOrdersByUserId(user.getId());
            user.setOrders(orders);
        }
        return ResponseHelper.buildResponseModel(usersMapper.getAllUsers(p).setRecords(users));
    }
}
